package org.misalen.web.server.controllers.driver;

import java.math.BigInteger;
import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.web.database.model.mysql.pro.ProDriverUser;
import org.misalen.web.database.model.mysql.pro.ProDriverUserExtractCash;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.driver.DriverWithdrawalApplyForm;
import org.misalen.web.server.forms.driver.DriverWithdrawalRecordForm;
import org.misalen.web.server.result.Empty;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.sms.info.AliInfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "用户提现", description = "申请、记录", index = 400)
@RequestMapping(value = ServerUrl.DriverWithdrawalControllerUrl)
public class DriverWithdrawalController extends BaseController {

	@RequestMapping(value = "/apply", method = { RequestMethod.POST })
	@InterfaceApi(value = "提现申请", resultClass = { Empty.class })
	public @ResponseBody ServerResult apply(@RequestBody DriverWithdrawalApplyForm form) throws CustomerException {
		FormUtil.validate(form);
		ProDriverUser driverUser = proDriverUserRepository.get(ProDriverUser.class, form.getUserId());
		if (driverUser == null) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10005);
		} else if (driverUser.getBalance() < form.getAmount()) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10010);
		} else {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(driverUser.getMobile(), form.getAuthCode(),
					Uncertain.smsValidity, AliInfoType.driverWithDrawCode.type);
			if (sendByTimeCount == 0) {
				return ServerResult.error(ErrorCode.SYS_ERROR_CODE_00003);
			} else {
				proDriverUserRepository.updateBalance(-form.getAmount().intValue(), driverUser.getPrimaryKey());
				ProDriverUserExtractCash driverUserExtractCash = new ProDriverUserExtractCash();
				driverUserExtractCash.setAddTime(new Date());
				driverUserExtractCash.setAmount(BigInteger.valueOf(form.getAmount()));
				driverUserExtractCash.setAccount(form.getAccount());
				driverUserExtractCash.setType(form.getProExtractType());
				driverUserExtractCash.setDriverUserId(driverUser.getPrimaryKey());
				driverUserExtractCash.setState("not-operating");
				proDriverUserExtractCashRepository.save(driverUserExtractCash);
			}
		}
		return ServerResult.ok();
	}

	@RequestMapping(value = "/record", method = { RequestMethod.POST })
	@InterfaceApi(value = "提现记录", resultClass = { PaginationFrom.class, ProDriverUserExtractCash.class }, include = {
			"amount", "account", "state", "type", "addTime", "processingTime" })
	public PaginationFrom<ProDriverUserExtractCash> record(@RequestBody DriverWithdrawalRecordForm form)
			throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProDriverUserExtractCash> pagination = formToPage(form, ProDriverUserExtractCash.class);
		ProDriverUserExtractCash allclass = new ProDriverUserExtractCash();
		allclass.setDriverUserId(form.getUserId());
		if (form.getProExtractState() != null) {
			allclass.setState(form.getProExtractState());
		}
		return proDriverUserExtractCashRepository.findPageByBean(allclass, pagination);
	}

}
